//
//  HJTeamVC.m
//  project
//
//  Created by hu on 2019/4/4.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJTeamVC.h"
#import "HJTeamInvitation.h"
#import "HJFightVC.h"
#import "YFDatePickerView.h"
#import "HJTeamDetailVC.h"
#import "HJReceivedDetailVC.h"
#import "HJMapVC.h"
#import "YFJourneyModel.h"
#import "YFMapSearchModel.h"
#import "HJTeamAvatorView.h"
#import "HJJourneyHeaderView.h"
#import "YFMapSearchVC.h"
#import "YFLikeModel.h"
#import "HJChatTextMsgModel.h"


@interface HJTeamVC ()<HJTeamInvitationDelegate>
@property (nonatomic,strong) HJTeamInvitation * teamInvitation;
/**
 Header View
 */
@property (nonatomic, strong, nonnull) HJJourneyHeaderView *headerView;
/**
 Header 头像 View
 */
@property (nonatomic, strong, nonnull) HJTeamAvatorView *avatorView;

@property (nonatomic, strong) YFMapSearchModel * mapModel;
@property (nonatomic, strong) NSString * timeStamp;
@property (nonatomic, assign) BOOL isEdit;
@end

@implementation HJTeamVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"邀约同行";

    if (_UIType==UIContentTypeEdite) {
        self.mapModel = [[YFMapSearchModel alloc]init];
        self.mapModel.latitude = self.simpleModel.data.team.startLat.longLongValue;
        self.mapModel.longitude = self.simpleModel.data.team.startLng.longLongValue;
        self.mapModel.POIName = self.simpleModel.data.team.startText;
    }
}

-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    self.tableView.tableHeaderView = [self tableViewWithHeaderView];
}


-(HJJourneyHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[HJJourneyHeaderView alloc]init];
        if (self.isChat) {
            _headerView.model = self.journeyModel;// 聊天进来的行程
        }else {
            _headerView.journeyModel = self.recordModel;// 其它行程进来的
        }
    }
    return _headerView;
}

-(HJTeamAvatorView *)avatorView {
    if (!_avatorView) {
        _avatorView = [[HJTeamAvatorView alloc]init];
        if (self.isChat) {
            _avatorView.simpleModel = self.simpleModel;// 聊天进来的组队头像
        }else {
            _avatorView.recordModel = self.recordModel;// 其它行程进来的
        }
    }
    return _avatorView;
}

//341 278
-(UIView *)tableViewWithHeaderView {
    UIView * head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH)];
    head.backgroundColor = [UIColor clearColor];
    switch (_UIType) {
        case UIContentTypeNormal:
        {
            self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, JOURNEY_HEADER_HEIGHT);
            [head addSubview:self.headerView];
        }
            break;
        case UIContentTypeAgree:
        {
            self.avatorView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
            [head addSubview:self.avatorView];
            self.headerView.frame= CGRectMake(0, CGRectGetMaxY(self.avatorView.frame), SCREEN_WIDTH, JOURNEY_HEADER_HEIGHT);
            [head addSubview:self.headerView];
        }
            break;
        default:
            self.headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, JOURNEY_HEADER_HEIGHT);
            [head addSubview:self.headerView];
            break;
    }
    HJTeamInvitation * teamInvitation = [[HJTeamInvitation alloc]init];
    teamInvitation.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), SCREEN_WIDTH, 175);
    teamInvitation.delegate = self;
    if (self.isChat) {
        teamInvitation.simpleModel = self.simpleModel;
    }
    self.teamInvitation = teamInvitation;
    [head addSubview:teamInvitation];
    // 创建底部按钮
    [self setUpBtmViewWithHeadView:head];
    
    return head;
}

/**
 初始化创建底部视图按钮
 */
-(void)setUpBtmViewWithHeadView:(UIView *)view {
    switch (self.UIType) {
        case UIContentTypeNormal:
        {
            UIButton * peerBtn = [self configButtonWithTitle:@"邀约同行" bgColor:[UIColor blackColor] titleColor:[UIColor whiteColor]];
            peerBtn.frame = CGRectMake(10, CGRectGetMaxY(self.teamInvitation.frame)+100, SCREEN_WIDTH-20, 42);
            @weakify(self)
            [[peerBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self)
                [self publicTeamWorkData];
            }];
            [view addSubview:peerBtn];
        }
            break;
        case UIContentTypeEdite:
        {
            UIButton * agreeBtn = [self configButtonWithTitle:@"同意" bgColor:[UIColor blackColor] titleColor:[UIColor whiteColor]];
            agreeBtn.frame = CGRectMake(10, CGRectGetMaxY(self.teamInvitation.frame)+100, SCREEN_WIDTH-20, 42);
            @weakify(self)
            [[agreeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self)
                [self agreeTeamWorkData];
            }];
            [view addSubview:agreeBtn];
            
            UIButton * refusedBtn = [self configButtonWithTitle:@"拒绝" bgColor:[UIColor whiteColor] titleColor:[UIColor blackColor]];
            refusedBtn.frame= CGRectMake(10, agreeBtn.bottom+20, (SCREEN_WIDTH-20-45)/2, 42);
            [[refusedBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self)
                [self refusedTeamWorkData];
            }];
            [view addSubview:refusedBtn];
            
            UIButton * editBtn = [self configButtonWithTitle:@"修改" bgColor:[UIColor whiteColor] titleColor:[UIColor blackColor]];
            editBtn.frame = CGRectMake(refusedBtn.right+45, agreeBtn.bottom+20, (SCREEN_WIDTH-20-45)/2, 42);
            [[editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self)
                [self editeTeamWorkData];
            }];
            [view addSubview:editBtn];
        }
             break;
        case UIContentTypeMe:
        {
            @weakify(self)
            UIButton * EditMeBtn = [self configButtonWithTitle:@"修改同行信息" bgColor:[UIColor whiteColor] titleColor:[UIColor blackColor]];
            EditMeBtn.frame = CGRectMake(10, CGRectGetMaxY(self.teamInvitation.frame)+100, SCREEN_WIDTH-20, 42);
            [[EditMeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self)
                [self editeTeamWorkData];
            }];
            [view addSubview:EditMeBtn];
            
            UIButton * tipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            tipBtn.titleLabel.font = [UIFont systemFontOFSize14];
//            [tipBtn setTitle:[NSString stringWithFormat:@"让%@再想想...",self.simpleModel.data.user.nickName] forState:0];
            //对方再想想
            [tipBtn setTitle:[NSString stringWithFormat:@"让%@再想想...",self.simpleModel.data.userB.nickName] forState:0];
            [tipBtn setTitleColor:[UIColor colorWithHex:0x999999] forState:0];
            tipBtn.frame = CGRectMake(10, EditMeBtn.bottom+20, SCREEN_WIDTH-20, 42);
            tipBtn.userInteractionEnabled = NO;
            [view addSubview:tipBtn];
        }
            break;
        default:
            break;
    }
}

- (UIButton *)configButtonWithTitle:(NSString *)title bgColor:(UIColor *)bgColor titleColor:(UIColor *)titleColor {
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setBackgroundColor:bgColor];
    [btn setTitleColor:titleColor forState:0];
    [btn setTitle:title forState:(UIControlStateNormal)];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    btn.adjustsImageWhenHighlighted = NO;
    [self.view setupShadowWithView:btn];
    
    return btn;
}

-(void)meetingTimeWithTextField:(UITextField *)textField timeLabel:(UILabel *)timeLabel {
    LKLog(@"见面时间");
    self.isEdit = YES;
    YFDatePickerView * datePicker = [YFDatePickerView datePickerView];
    [datePicker setSelectCompleted:^(NSString * _Nonnull year, NSString * _Nonnull month, NSString * _Nonnull day, NSString * _Nonnull hour, NSString * _Nonnull minute) {
        timeLabel.text =[NSString stringWithFormat:@"%@.%@ %@:%@", month, day, hour, minute];
        self.timeStamp = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",year , month, day, hour, minute];
    }];
    [datePicker show];
}
-(void)meetingLocationWithTextField:(UITextField *)textField locationLabel:(UILabel *)locationLabel {
    LKLog(@"见面地点");
    self.isEdit = YES;
    @weakify(self)
    YFMapSearchVC * mapVC =[YFMapSearchVC new];
    mapVC.searchCompleted = ^(YFMapSearchModel * _Nonnull model) {
        @strongify(self)
        locationLabel.text = [NSString stringWithFormat:@"%@",model.POIName];
        self.mapModel = model;
    };
    [self.navigationController pushViewController:mapVC animated:YES];
}

#pragma mark -- 发布组队接口调用
-(void)publicTeamWorkData {
    if (!self.teamInvitation.timeLabel.text.length) {
        [self showErrorHUDWithError:@"请选择见面时间"];
        return;
    }
    if (!self.teamInvitation.locationLabel.text.length) {
        [self showErrorHUDWithError:@"请选择见面地点"];
        return;
    }
    
    NSString * url = [Api stringByAppendingString:TeamAddApi];
    [HJHttpTool post:url params:[self params] success:^(HJResponeds *result) {
        [self showSuccessHUDWithStr:result.resultDesc];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(id result) {
        [self showErrorHUDWithError:result];
    }];
}


#pragma mark -- 修改组队
-(void)editeTeamWorkData {
    if (!self.isEdit) return;
    NSString * url = [Api stringByAppendingString:TeamModifyApi];
    [HJHttpTool post:url params:[self params] success:^(HJResponeds *result) {
        [self showSuccessHUDWithStr:result.resultDesc];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(id result) {
        [self showErrorHUDWithError:result];
    }];
}

-(NSMutableDictionary *)params {
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"startLat"] = [NSString stringWithFormat:@"%lf",self.mapModel.latitude];//见面地点维度
    params[@"startLng"] = [NSString stringWithFormat:@"%lf",self.mapModel.longitude]; //见面地点经度
    params[@"startText"] = self.mapModel.POIName; // 见面地点
    params[@"startTime"] = [self.timeStamp stringByAppendingString:@":00"];// 见面时间
    NSString *tripAId;
    NSString *userAId;
    NSString *tripBId;
    NSString *userBId;
    NSInteger wayId;
    if (_UIType==UIContentTypeNormal && self.isChat) {
        tripAId = self.journeyModel.journey_id;
        userAId = self.journeyModel.userId;
        tripBId = self.otherModel.journey_id;
        userBId = self.otherModel.userId;
        wayId = self.journeyModel.wayId;
    }else {
        tripAId = self.simpleModel.data.team.tripAId;
        userAId = self.simpleModel.data.team.userAId;
        tripBId = self.simpleModel.data.team.tripBId;
        userBId = self.simpleModel.data.team.userBId;
        wayId = self.simpleModel.data.team.wayId.integerValue;
    }
    params[@"tripAId"] = self.isChat ?  tripAId : self.recordModel.tripModel.journey_id; //发起组队用户行程Id
    params[@"userAId"] = self.isChat ?  userAId : [HJUserManager getUserModel].userId; //发起组队用户Id
    params[@"tripBId"] = self.isChat ?  tripBId : self.otherRecordModel.trip.like_trip_id;
    params[@"userBId"] = self.isChat ?  userBId : self.otherRecordModel.user.userId; //被邀请组队用户Id
    params[@"wayId"] = self.isChat ?  @(wayId) : @(self.recordModel.tripModel.wayId);
    params[@"id"] = self.simpleModel.data.team.simple_id;
    
    return params;
}

#pragma mark -- 同意组队
-(void)agreeTeamWorkData {
    NSString * url = [Api stringByAppendingString:TeamAgreeApi];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"teamId"] = self.simpleModel.data.team.simple_id;
    params[@"userId"] = [HJUserManager getUserModel].userId;
    [HJHttpTool post:url params:params success:^(HJResponeds *result) {
        [self showSuccessHUDWithStr:result.resultDesc];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(id result) {
        [self showErrorHUDWithError:result];
    }];
}
#pragma mark -- 拒绝组队
-(void)refusedTeamWorkData {
    NSString * url = [Api stringByAppendingString:TeamRefusedApi];
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"teamId"] = self.simpleModel.data.team.simple_id;
    [HJHttpTool get:url params:params success:^(HJResponeds *result) {
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(id result) {
        [self showErrorHUDWithError:result];
    }];
}


@end
